package com.zqh.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * 定义返回结果（json格式）
 * 失败：状态码 描述信息 查询的数据信息
 * 成功：状态码 描述信息 查询的数据信息（用户的个人信息）
 */
@Data
public class AppResult<T> {

    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) //不论任何情况都要参与JSON的序列化
    private int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    //返回的具体数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public AppResult(int code, String message) {
        this(code,message,null);
    }

    /**
     * 成功
     * @return
     */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> AppResult<T> success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> AppResult<T> success(String message,T data){
        return new AppResult(ResultCode.SUCCESS.getCode(),message,message);
    }

    /**
     * 失败
     * @return
     */
    public static AppResult failed (){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }
    public static AppResult failed (String message){
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }
    public static AppResult failed (ResultCode resultCode){
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }

}
